﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhotoGallery.DAL;
using PhotoGallery.Model;
namespace PhotoGallery.DLL
{
    public class RoleDataAccess
    {
        roleDataContext rdc = new roleDataContext();
        /*添加*/
        public void insert(int roleid, string rolename)
        {
            role role = new role();
            role.roleID = roleid;
            role.roleName = rolename;
            rdc.roles.InsertOnSubmit(role);
            rdc.SubmitChanges();
        }
        /*删除*/
        public void delete(int roleid) 
        {
            var result = from m in rdc.roles
                         where m.roleID == roleid
                         select m;
            foreach (role m in result)
                rdc.roles.DeleteOnSubmit(m);
            rdc.SubmitChanges();
        }
        /*查询*/
        public Role searchRoleByID(int roleid)
        {
            var query = rdc.roles.Where(m => m.roleID ==roleid).Select(m => new Role(m.roleID,m.roleName));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*修改*/
        public void modifyRole(int roleid, string rolename) 
        {
            var result = from m in rdc.roles
                         where m.roleID== roleid
                         select m;
            foreach (role r in result)
            {
                r.roleName = rolename;
            }
            rdc.SubmitChanges();
        }
    }
}
